package com.example.iceman.stepcounterapp;

import android.content.Context;
import android.database.Cursor;

import com.example.iceman.stepcounterapp.db.StepDataBase;
import com.example.iceman.stepcounterapp.db.StepDataBase_Table;
import com.raizlabs.android.dbflow.sql.language.Method;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import java.util.ArrayList;
import java.util.List;

/**
 * 本地存储步数
 */
public class StepSaveManager {
    private String user = "default";

    private StepSaveManager(Context context) {
    }


    private static StepSaveManager sInstance;

    public static StepSaveManager getInstance() {
        if (sInstance == null) {
            sInstance = new StepSaveManager(BaseApplication.getAppContext());
        }
        return sInstance;
    }

    public void setUser(String user) {
        this.user = user;
    }

    /**
     * 插入数据库
     *
     * @param data
     */
    public void insertStepRecord(StepDataBase data) {
        data.upload = false;
        data.user = user;
        data.save();
    }

    /**
     * 更新数据库中的记录,将指定记录设置为已上传
     *
     * @param data 只需要对year,day,hour进行赋值即可确定某一条记录
     */
    public void setStepRecordUploaded(StepDataBase data) {
        data.upload = true;
        data.update();
    }


    public int getStepTotalByYear(int year) {
        Cursor cursor = SQLite.select(Method.sum(StepDataBase_Table.count).as("total"))
                .from(StepDataBase.class)
                .where(StepDataBase_Table.year.eq(String.valueOf(year)), StepDataBase_Table.user.eq(user))
                .query();
        if (cursor.moveToFirst()) {
            return cursor.getInt(0);
        } else {
            return 0;
        }
    }

    public int getStepTotalByMonth(int year, int month) {
        Cursor cursor = SQLite.select(Method.sum(StepDataBase_Table.count).as("total"))
                .from(StepDataBase.class)
                .where(StepDataBase_Table.year.eq(String.valueOf(year)), StepDataBase_Table.month.eq(String.valueOf(month)), StepDataBase_Table.user.eq(user))
                .query();
        if (cursor.moveToFirst()) {
            return cursor.getInt(0);
        } else {
            return 0;
        }

    }

    public int getStepTotalByWeek(int year, int week) {
        Cursor cursor = SQLite.select(Method.sum(StepDataBase_Table.count).as("total"))
                .from(StepDataBase.class)
                .where(StepDataBase_Table.year.eq(String.valueOf(year)), StepDataBase_Table.week.eq(String.valueOf(week)), StepDataBase_Table.user.eq(user))
                .query();
        if (cursor.moveToFirst()) {
            return cursor.getInt(0);
        } else {
            return 0;
        }
    }

    public int getStepTotalByDay(int year, int day) {
        Cursor cursor = SQLite.select(Method.sum(StepDataBase_Table.count).as("total"))
                .from(StepDataBase.class)
                .where(StepDataBase_Table.year.eq(String.valueOf(year)), StepDataBase_Table.day.eq(String.valueOf(day)), StepDataBase_Table.user.eq(user))
                .query();
        if (cursor.moveToFirst()) {
            return cursor.getInt(0);
        } else {
            return 0;
        }
    }

    public ArrayList<StepDataBase> getStepDataByDay(int year, int day) {
        List<StepDataBase> record = SQLite.select()
                .from(StepDataBase.class)
                .where(StepDataBase_Table.year.eq(String.valueOf(year)), StepDataBase_Table.day.eq(String.valueOf(day)), StepDataBase_Table.user.eq(user))
                .queryList();
        return (ArrayList<StepDataBase>) record;
    }

    /**
     * @param needall
     * @return
     */
    public ArrayList<StepDataBase> getStepsOfNoUpload(boolean needall) {
        List<StepDataBase> record = SQLite.select()
                .from(StepDataBase.class)
                .where(StepDataBase_Table.upload.eq(Boolean.FALSE), StepDataBase_Table.user.eq(user))
                .queryList();

        return (ArrayList<StepDataBase>) record;
    }

}
